import { Component } from '@angular/core';
import { IonicPage, NavController, ViewController } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { Md5 } from "ts-md5/dist/md5";
import { user_info } from '../login/login';
import { App } from 'ionic-angular';
import { NavParams } from 'ionic-angular/navigation/nav-params';


/**
 * Generated class for the ChangepwdPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
	selector: 'page-changepwd',
	templateUrl: 'changepwd.html',
})
export class ChangepwdPage {

	// ///验证码倒计时
	// public verifyCode: any = {
	// 	verifyCodeTips: "获取验证码",
	// 	countdown: 120, //总共时间
	// 	disable: true
	// };

	phone_name: any;
	phone_name2: any;//隐藏的手机号码
	member_id: any;
	new_pwd: string;
	confirm_pwd: string;
	veritycode: string;
	sendStatus:any;
	//用户信息
	userInfo: user_info = new user_info();

	//短信倒计时
	date = 0;

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public viewCtrl: ViewController,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public user: UserInfoProvider,
		private app: App) { 
			this.sendStatus = navParams.get('sendStatus');
		}

	ionViewDidLoad() {
		console.log('ionViewDidLoad ChangepwdPage');
		this.userInfo = this.user.getuserInfo();
		console.log(this.userInfo);
		this.member_id = this.userInfo.member_id;
		this.phone_name = this.userInfo.phone;
		this.phone_name2 = this.phone();
		console.log(this.phone_name2);
		// this.storage.getStorage("login_info").then(res => {
		// 	if(res != undefined) {
		// 		this.member_id=res['member_id']	;
		// 		this.phone_name =res['phone'];
		// 		this.phone_name2=this.phone()
		// 		console.log(this.phone_name2);
		// 	}
		// });


	}

	//隐藏手机号码
	phone() {
		//phone1截取手机号前3位
		var phone1 = this.phone_name.substring(3, 0)
		//var phone1=this.phone_name.substring(0,3)//截取时包括前不包括后面，下标从0开始
		console.log(phone1)
		//phone2隐藏手机号
		var phone2 = "****"
		//phone3截取到手机号7-11位，下标从0开始
		var phone3 = this.phone_name.substring(7)
		console.log(phone3)
		var phone4 = phone1 + phone2 + phone3
		return phone4;

	}

	/**
	* 登陆（重设密码）
	*/
	saveChange() {
		if (this.veritycode == '' || this.veritycode == null) {
			this.dialog.showAlert('请输入验证码', '');
			return;
		}
		if (this.new_pwd == '' || this.new_pwd == null) {
			this.dialog.showAlert('请输入密码', '');
			return;
		}
		if (this.confirm_pwd == '' || this.confirm_pwd == null) {
			this.dialog.showAlert('请输入确认密码', '');
			return;
		}
		if (this.new_pwd.length < 6 || this.new_pwd.length > 16 || this.confirm_pwd.length < 6 || this.confirm_pwd.length > 16) {
			this.dialog.showAlert('密码必须6-16位', '');
			return;
		}
		if (this.veritycode == '' || this.veritycode == null) {
			this.dialog.showAlert('请输入验证码', '');
			return;
		}
		if (this.veritycode.length < 4 || this.veritycode.length > 6) {
			this.dialog.showAlert('验证码是4-6位', '');
			return;
		}
		// else if(this.new_pwd.length!=6||this.confirm_pwd.length!=6){
		// 	this.dialog.showAlert('警告','密码只能6位');
		// 	return;
		// }
		if (this.new_pwd == this.confirm_pwd) {
			var params = {
				phone: this.phone_name,
				newpassword: this.new_pwd,
				code: this.veritycode
			};
			this.http.postJson('', 'member/member/signup', {
				'phone': params.phone,
				// 'password': Md5.hashStr(params.newpassword).toString(),md5加密格式
				"password": params.newpassword,
				"vcode": params.code
			}, '')
				.then(res => {
					if (res['status'] == 200) {
						this.dialog.showAlert(res['message'], '');
						this.app.getRootNav().setRoot('LoginPage');

					} else {
						this.dialog.showAlert(res['message'], '');

					}
				});
		} else {
			this.dialog.showAlert('两次输入的密码不一致', '');
		}

	}

	getSMS() {
		var subscriber = /^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\d{8}$/;
		var silly = subscriber.test(this.phone_name);
		if (!silly) {
			this.dialog.showAlert('手机号不合法', '');
			return;
		}
		this.http.postJson('', 'users/member/sms',
			{
				mobiles: this.phone_name,
				type:"changepwd",
				sendStatus:this.sendStatus,
				send_from: 4
			}, '')
			.then(res => {
				let form = this;
				if (res['status'] === 200) {
					this.date = 60;
					const interval = setInterval(function () {
						form.date--;
						if (form.date === 0) { clearInterval(interval); }
					}, 1000);

				}
			});
	}
}
